package com.youkeyi.ddy.cloud.application.business.hr.employee.employee.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.employee.BusinessEmployeeRemindCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.employee.dto.*;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.employee.param.BusinessContractExpiresEmployeeListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.employee.param.BusinessProbationaryPeriodExpiresEmployeeListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.hr.employee.employee.param.BusinessRecentBirthdaysEmployeeListCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.HrEmployeeContractCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.dto.HrEmployeeContractListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.dto.HrEmployeeContractListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.contract.param.HrEmployeeContractQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.HrEmployeeCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.dto.HrEmployeeListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.dto.HrEmployeeListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.hr.employee.employee.param.HrEmployeeQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.PublicOrganizationCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.dto.PublicOrganizationListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.dto.PublicOrganizationListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.organization.param.PublicOrganizationQueryCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.utils.SystemDateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * BusinessEmployeeRemindController
 *
 * @Author liuxiawang
 * @Date 2024/4/30 3:28 PM
 * @Location shenzhen.china
 */
@Service
public class BusinessEmployeeRemindController implements BusinessEmployeeRemindCloudService {

    @Autowired
    private HrEmployeeCloudService hrEmployeeCloudService;
    @Autowired
    private PublicOrganizationCloudService publicOrganizationCloudService;
    @Autowired
    private HrEmployeeContractCloudService hrEmployeeContractCloudService;

    @Override
    public BusinessRecentBirthdaysEmployeeListCloudDto recentBirthdaysEmployeeList(BusinessRecentBirthdaysEmployeeListCloudParam param) {
        BusinessRecentBirthdaysEmployeeListCloudDto dto = new BusinessRecentBirthdaysEmployeeListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        HrEmployeeQueryCloudParam employeeQueryCloudParam = new HrEmployeeQueryCloudParam();
         employeeQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        if (CollectionUtils.isNotEmpty(param.getEmployeeCodes()) || CollectionUtils.isNotEmpty(param.getEmployeeStatuses())
                || StringUtils.isNotBlank(param.getNameFuzzy())
                || null != param.getBirthdayBegin() || null != param.getBirthdayEnd()
        ) {
            employeeQueryCloudParam.setEmployeeCodes(param.getEmployeeCodes());
            employeeQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
            employeeQueryCloudParam.setEmployeeStatuses(param.getEmployeeStatuses());
            employeeQueryCloudParam.setBirthdayBegin(param.getBirthdayBegin());
            employeeQueryCloudParam.setBirthdayEnd(param.getBirthdayEnd());
            employeeQueryCloudParam.setPageSize(param.getPageSize());
            employeeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        } else {
            employeeQueryCloudParam.setBirthdayBegin(param.getBirthdayBegin());
            employeeQueryCloudParam.setBirthdayEnd(param.getBirthdayEnd());
            employeeQueryCloudParam.setPageSize(5);
            employeeQueryCloudParam.setCurrentPage(1);
        }
        HrEmployeeListCloudDto employeeDtos = hrEmployeeCloudService.queryForBirthday(employeeQueryCloudParam);
        if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
            List<String> departmentKeys = employeeDtos.getRows().stream().map(HrEmployeeListItemCloudDto::getDepartmentKey).collect(Collectors.toList());
            PublicOrganizationQueryCloudParam organizationQueryCloudParam = new PublicOrganizationQueryCloudParam();
            organizationQueryCloudParam.setOrganizationPrimaryKeys(departmentKeys);
            PublicOrganizationListCloudDto organizationDtos = publicOrganizationCloudService.query(organizationQueryCloudParam);
            Map<String, String> departmentMap = new HashMap<>();
            if (null != organizationDtos && CollectionUtils.isNotEmpty(organizationDtos.getRows())) {
                departmentMap.putAll(organizationDtos.getRows().stream().collect(Collectors.toMap(PublicOrganizationListItemCloudDto::getOrganizationPrimaryKey, PublicOrganizationListItemCloudDto::getName)));
            }

            List<BusinessRecentBirthdaysEmployeeListItemCloudDto> items = new ArrayList<>();
            for (HrEmployeeListItemCloudDto employeeDto : employeeDtos.getRows()) {
                BusinessRecentBirthdaysEmployeeListItemCloudDto item = new BusinessRecentBirthdaysEmployeeListItemCloudDto();
                item.setAccount(employeeDto.getAccount());
                item.setBirthday(employeeDto.getBirthday());
                item.setName(employeeDto.getName());
                item.setZodiac(employeeDto.getZodiac());
                item.setConstellation(employeeDto.getConstellation());
                item.setDepartmentKey(employeeDto.getDepartmentKey());
                item.setDepartment(departmentMap.get(employeeDto.getDepartmentKey()));
                item.setEmployeePrimaryKey(employeeDto.getPrimaryKey());
                item.setEmployeePrimaryKey(employeeDto.getPrimaryKey());
                items.add(item);
            }
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public BusinessProbationaryPeriodExpiresEmployeeListCloudDto probationaryPeriodExpiresEmployeeList(BusinessProbationaryPeriodExpiresEmployeeListCloudParam param) {
        BusinessProbationaryPeriodExpiresEmployeeListCloudDto dto = new BusinessProbationaryPeriodExpiresEmployeeListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        HrEmployeeQueryCloudParam employeeQueryCloudParam = new HrEmployeeQueryCloudParam();
        // employeeQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        if (CollectionUtils.isNotEmpty(param.getEmployeeCodes()) || CollectionUtils.isNotEmpty(param.getEmployeeStatuses())
                || StringUtils.isNotBlank(param.getNameFuzzy())
                || null != param.getProbationaryPeriodBegin() || null != param.getProbationaryPeriodEnd()
        ) {
            employeeQueryCloudParam.setEmployeeCodes(param.getEmployeeCodes());
            employeeQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
            employeeQueryCloudParam.setEmployeeStatuses(param.getEmployeeStatuses());
            employeeQueryCloudParam.setRegularizationDateBegin(param.getProbationaryPeriodBegin());
            employeeQueryCloudParam.setRegularizationDateEnd(param.getProbationaryPeriodEnd());
            employeeQueryCloudParam.setPageSize(param.getPageSize());
            employeeQueryCloudParam.setCurrentPage(param.getCurrentPage());
        } else {
            employeeQueryCloudParam.setRegularizationDateBegin(param.getProbationaryPeriodBegin());
            employeeQueryCloudParam.setRegularizationDateEnd(param.getProbationaryPeriodEnd());
            employeeQueryCloudParam.setPageSize(5);
            employeeQueryCloudParam.setCurrentPage(1);
        }

        HrEmployeeListCloudDto employeeDtos = hrEmployeeCloudService.query(employeeQueryCloudParam);
        if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
            List<String> departmentKeys = employeeDtos.getRows().stream().map(HrEmployeeListItemCloudDto::getDepartmentKey).collect(Collectors.toList());
            PublicOrganizationQueryCloudParam organizationQueryCloudParam = new PublicOrganizationQueryCloudParam();
            organizationQueryCloudParam.setOrganizationPrimaryKeys(departmentKeys);
            PublicOrganizationListCloudDto organizationDtos = publicOrganizationCloudService.query(organizationQueryCloudParam);
            Map<String, String> departmentMap = new HashMap<>();
            if (null != organizationDtos && CollectionUtils.isNotEmpty(organizationDtos.getRows())) {
                departmentMap.putAll(organizationDtos.getRows().stream().collect(Collectors.toMap(PublicOrganizationListItemCloudDto::getOrganizationPrimaryKey, PublicOrganizationListItemCloudDto::getName)));
            }

            List<BusinessProbationaryPeriodExpiresEmployeeListItemCloudDto> items = new ArrayList<>();
            employeeDtos.getRows().forEach(employeeDto -> {
                BusinessProbationaryPeriodExpiresEmployeeListItemCloudDto item = new BusinessProbationaryPeriodExpiresEmployeeListItemCloudDto();
                item.setEmployeePrimaryKey(employeeDto.getEmployeePrimaryKey());
                item.setName(employeeDto.getName());
                item.setAccount(employeeDto.getAccount());
                item.setProbationPeriodDate(employeeDto.getRegularizationDate());
                item.setDepartmentKey(employeeDto.getDepartmentKey());
                item.setEmployeePrimaryKey(employeeDto.getPrimaryKey());
                item.setDepartment(departmentMap.get(employeeDto.getDepartmentKey()));
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public BusinessContractExpiresEmployeeListCloudDto contractExpiresEmployeeList(BusinessContractExpiresEmployeeListCloudParam param) {
        BusinessContractExpiresEmployeeListCloudDto dto = new BusinessContractExpiresEmployeeListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        HrEmployeeContractQueryCloudParam employeeContractQueryCloudParam = new HrEmployeeContractQueryCloudParam();
        if (null == param.getContractBegin() || null == param.getContractEnd()) {
            employeeContractQueryCloudParam.setEndDateBegin(System.currentTimeMillis());
            employeeContractQueryCloudParam.setEndDateEnd(SystemDateUtils.endTimestamp(System.currentTimeMillis(), -1));
        } else {
            HrEmployeeQueryCloudParam employeeQueryCloudParam = new HrEmployeeQueryCloudParam();
            // employeeQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
            employeeQueryCloudParam.setEmployeeCodes(param.getEmployeeCodes());
            employeeQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
            employeeQueryCloudParam.setEmployeeStatuses(param.getEmployeeStatuses());
            HrEmployeeListCloudDto employeeDtos = hrEmployeeCloudService.query(employeeQueryCloudParam);
            List<String> employeePrimaryKeys = new ArrayList<>();
            if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
                employeePrimaryKeys.addAll(employeeDtos.getRows().stream().map(HrEmployeeListItemCloudDto::getPrimaryKey).collect(Collectors.toList()));
            }
            if (CollectionUtils.isEmpty(employeePrimaryKeys)) {
                dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
                return dto;
            }

            employeeContractQueryCloudParam.setEndDateBegin(param.getContractBegin());
            employeeContractQueryCloudParam.setEndDateEnd(param.getContractEnd());
            employeeContractQueryCloudParam.setEmployeePrimaryKeys(employeePrimaryKeys);
        }
        HrEmployeeContractListCloudDto contractDtos = hrEmployeeContractCloudService.query(employeeContractQueryCloudParam);
        if (null != contractDtos && CollectionUtils.isNotEmpty(contractDtos.getRows())) {
            List<String> employeePrimaryKeys = contractDtos.getRows().stream().map(HrEmployeeContractListItemCloudDto::getEmployeeKey).collect(Collectors.toList());

            HrEmployeeQueryCloudParam employeeQueryCloudParam = new HrEmployeeQueryCloudParam();
            employeeQueryCloudParam.setEmployeePrimaryKeys(employeePrimaryKeys);
            HrEmployeeListCloudDto employeeDtos = hrEmployeeCloudService.query(employeeQueryCloudParam);
            Map<String, HrEmployeeListItemCloudDto> employeeMap = new HashMap<>();
            if (null != employeeDtos && CollectionUtils.isNotEmpty(employeeDtos.getRows())) {
                employeeMap.putAll(employeeDtos.getRows().stream().collect(Collectors.toMap(HrEmployeeListItemCloudDto::getPrimaryKey, Function.identity(), (k1, k2) -> k1)));
            }

            List<BusinessContractExpiresEmployeeListItemCloudDto> items = new ArrayList<>();
            contractDtos.getRows().forEach(contractDto -> {
                BusinessContractExpiresEmployeeListItemCloudDto item = new BusinessContractExpiresEmployeeListItemCloudDto();
                HrEmployeeListItemCloudDto employee = employeeMap.get(contractDto.getEmployeeKey());
                item.setEmployeePrimaryKey(employee.getPrimaryKey());
                item.setEmployeePrimaryKey(employee.getPrimaryKey());
                item.setName(employee.getName());
                item.setZodiac(employee.getZodiac());
                item.setConstellation(employee.getConstellation());
                item.setAccount(employee.getAccount());
                item.setStartDate(contractDto.getStartDate());
                item.setEndDate(contractDto.getEndDate());
                item.setContractName(contractDto.getContractName());
                items.add(item);
            });
            dto.setRows(items);
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }
}
